package com.waiting.leetcode;

/**
 * @Description
 * @Author bjweitingting
 * @Date 2019/3/15
 **/
public class SingleNumberIII260 {

    public static void main(String[] args) {
        int[] test = {1, 2, 1, 3, 2, 5};
        System.out.println(singleNumber(test).toString());
    }

    public static int[] singleNumber(int[] nums) {
        int[] result = new int[2];
        int tmp = 0;
        for (int i = 0; i < nums.length; i++) {
            tmp ^= nums[i];
        }
        //找最右边的值
        int flag = 1;
        while (true) {
            if ((1 & tmp) != 0) {
                break;
            } else {
                flag = flag << 1;
                tmp = tmp >> 1;
            }
        }
        for (int i = 0; i < nums.length; i++) {
            if ((nums[i] & flag) == 0) {
                result[0] ^= nums[i];
            } else {
                result[1] ^= nums[i];
            }
        }
        return result;
    }
}
